package exam.test1;

import dto.Order;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Test1 {

    public static void main(String[] args) {
        List<Order> orders = new ArrayList<>();
        SAXReader saxReader = new SAXReader();
        try (
                InputStream inputStream = new FileInputStream("src/testData/orders.xml");

        ) {

//        byte[] bytes = new byte[1024 * 4];
//        int read = inputStream.read(bytes);
//        System.out.println(new String(bytes,0,read));
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.elements();

            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            for (Element element : elements) {
                Order order = new Order();
                order.setId(Integer.parseInt(element.attributeValue("id")));
                order.setName(element.elementText("name"));
                order.setTime(LocalDateTime.parse(element.elementText("time"), dateTimeFormatter));
                order.setPrice(Double.parseDouble(element.elementText("double")));
                orders.add(order);
            }
            System.out.println(orders);


            orders.stream().filter(order -> order.getTime().isBefore(LocalDateTime.now())).forEach(System.out::println);

            //最贵的订单
            System.out.println(orders.stream().max(((o1, o2) -> Double.compare(o1.getPrice(), o2.getPrice()))).get());

            List<Order> collect = orders.stream().sorted(((o1, o2) -> Double.compare(o2.getPrice(), o1.getPrice()))).collect(Collectors.toList());
            System.out.println(collect);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


